<?php


namespace App\Http\ModelDao\Job;


use App\Config\QueueConfig;
use App\Http\Library\Queue;
use App\Http\Model\Job\PfsHeadAddbatchtransferModel;

class PfsHeadAddbatchtransferDao
{

    /**
     * 划拨申请编号，系统内唯一
     *
     * @return string
     */
    public static function transferId()
    {
        usleep(20000);
        /*生成13位时间戳*/
        list($t1, $t2) = explode('.', microtime(true));
        $t2 = str_pad(substr($t2, 0, 2), 2, 0, STR_PAD_LEFT);
        return 'HB' . date('YmdHis', $t1) . 'MS' . $t2;
    }


    /**
     * 获取需要同步的transferList结构
     *
     * @param $data
     *
     * @return array
     */
    public static function transferList($data, &$sumTransferAmt, &$createDate, $last, &$transferDate)
    {
        $arr = [];
        foreach ($data['list'] as $val) {
            if (empty($createDate)) {
                $createDate = $val->create_date;
            }
            if (empty($transferDate)) {
                $transferDate = $val->transfer_date;
            }
            /*检查是否存在不是同一天的记录*/
            if ($createDate !== $val->create_date) {
                throw new \Exception('数仓-班级划拨计划表,存在差异日期!!!');
            }
            if ($transferDate !== $val->transfer_date) {
                throw new \Exception('数仓-班级划拨计划表,存在差异日期!!!');
            }
            /*查询当前本次划拨是否已经发起过了*/
            $transferListExist = PfsHeadAddbatchtransferModel::transferListExist($data['pfs_dw_id'], $val->sub_transfer_id);
            if (!empty($transferListExist)) {
                throw new \Exception('划拨子编号重复!');
            }

            /*注册班级余额刷新任务*/
            Queue::clock([
                'class_id'  => $val->class_id,
                'pfs_dw_id' => $data['pfs_dw_id'],
                /*取划拨的记录创建时间,跟班级信息是有区别的*/
                'date'      => $val->create_date,
            ], 60, QueueConfig::CLASS_AMT_REFRESH);
            /*划拨金额*/
            $transferAmt = $val->transfer_amt;
            /*减去本期已划拨金额 --数仓已处理,这里不需要再次处理了!!!*/
            /*if (!empty($last[$val->class_id])) {
                foreach ($last[$val->class_id] as $v) {
                    $transferAmt = bcsub($transferAmt, $v->transfer_amt);
                }
            }*/
            /*本次申请划拨总金额*/
            $sumTransferAmt = bcadd($sumTransferAmt, $transferAmt);
            $arr[] = [
                /*班级编号，同一机构下确保唯一*/
                'classId'            => $val->class_id,
                /*划拨申请子编号*/
                'subTransferId'      => $val->sub_transfer_id,
                /*划拨金额*/
                'transferAmt'        => $transferAmt,
                /*销课时长，若划拨类型为销课模式，则此参数必传。*/
                'eliminateClassHour' => $val->eliminate_class_hour,
                /*本次划拨比率，如30.0, 37.5, 100 模式01必填*/
                'rate'               => $val->rate,
                /*第几次划拨/解冻，如1,2,3 模式01必填*/
                'count'              => $val->count,
            ];
        }
        return $arr;
    }

}





/**
 * PfsHeadAddbatchtransferDao.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/7 18:19:00   操作:创建
 **/
